import { exportExcelOne } from "../../utils/request/api";
import msg from "../../utils/msg";
import { sr_data } from "../user/s_worker/util";
//辅导员与学生工作者端
// pages/dailySign/dailySign.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[] as sr_data[],
    data_origin:[] as sr_data[],
    notSign:0,
    hasSign:0,
    all:0,
    other:0,
    courseInfo:{}as {name:string,time:string},
  },
  onClickLeft(){
    wx.navigateBack();
  },
  changeActive(e:any){
    let index = e.detail.index;
    if(index==0){
      this.search('');
    }
    else if(index==1){
      this.search('已签到');
    }
    else if(index==2){
      this.search('待签到');
    }
    else if(index==3){
      this.search('其他');
    }
  },
  getInput(e:any){
    this.setData({
      input:e.detail.value,
    });
    this.search(e.detail.value);
  },
  search(input:string){
    msg.Loading('加载中');
    let data = this.data.data_origin;
    let showData=[] as sr_data[];
    data.forEach((item,index)=>{
      if(item.type==input || item.s_name.includes(input) || item.s_code.includes(input)){
        showData.push(item);
      }
      else if(input=='已签到'&&(item.type=='正常签到'||item.type=='迟到'))showData.push(item);
      else if(input=='其他'&&(item.type!='正常签到'&&item.type!='迟到'&&item.type!='待签到'))showData.push(item);
    });
    if(input=="") {
      showData=data;
    }
    this.setData({data:showData});
    msg.hideLoading();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option:any) {
    msg.Loading('加载中');
    let signDetail =JSON.parse(option.signDetail);
    let courseInfo=JSON.parse(option.courseInfo);
    this.setData({
      data:signDetail,
      data_origin:JSON.parse(option.signDetail),
      courseInfo:courseInfo,
    });
    this.cal();
    msg.hideLoading();
  },
  cal(){
    let data = this.data.data;
    let all=0;
    let hasSign=0;
    let notSign=0;
    let other=0;
    data.forEach((item)=>{
      all++;
      if(item.type=='待签到')notSign++;
      else if(item.type=='正常签到'||item.type=='迟到')hasSign++;
      else other++;
    });
    this.setData({
      all:all,
      hasSign:hasSign,
      notSign:notSign,
      other:other,
    });
  },
  download:async function(){
    await msg.confirm('确定导出签到数据吗','');
    wx.showLoading({
      title: '加载中',
    });
    let data = this.data.data[0];
    //{course_id:data.course_id,week:data.week,day:data.c_day,time:data.c_time}
    //data.c_name
    exportExcelOne({data:{course_id:data.course_id,week:data.week,day:data.c_day,time:data.c_time},fileName:data.c_name});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})